//app.js
var Util = require('utils/util.js');
App({
  onLaunch: function () {
  },
  getUserInfo: function (isrefresh, cb) {
    var that = this
    if (isrefresh != true && this.globalData.userInfo) {
      typeof cb == "function" && cb(this.globalData.userInfo)
    } else {
      wx.login({
        success: function (res) {
          var code = res.code
          //console.info(code);
          wx.getUserInfo({
            withCredentials: true,
            success: function (res) {
              var rawData = res['rawData'];
              var signature = res['signature'];
              var encryptData = res['encryptData'];
              var encryptedData = res['encryptedData']; //注意是encryptedData不是encryptData...坑啊
              var iv = res['iv'];
              console.info(res);
              Util.login({
                "theme": that.globalData.scene,
                "code": code,
                "rawData": rawData,
                "signature": signature,
                "encryptData": encryptData,
                'iv': iv,
                'encryptedData': encryptedData,
                'share_code': that.globalData.sharecode
              }, function (re) {
                console.info(re);
                wx.hideLoading();
                if (re.code == 200) {
                  wx.setStorage({
                    key: 'token',
                    data: re.token,
                    success: function (rr) {
                      console.info(re);
                      if (rr.errMsg == 'setStorage:ok') {
                        typeof cb == "function" && cb(re.token)
                      }
                    }
                  })
                }
                else {
                  Util.showMsgDialog("登录失败");
                }
              }, function (err) {
                Util.showMsgDialog("登录失败");
              })
            },
            fail: function (err) {
              console.log("wx.getUserInfo", err);
            }
          })
        },
        fail: function (err) {
          console.log("wx.login", err);
        }
      });
    }
  },
  globalData: {
    userInfo: null
  }
})